package cn.iocoder.yudao.module.crossborder.platform.controller.admin.platformwebhook;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.crossborder.platform.controller.admin.platformwebhook.vo.*;
import cn.iocoder.yudao.module.crossborder.platform.dal.dataobject.platformwebhook.PlatformWebhookDO;
import cn.iocoder.yudao.module.crossborder.platform.service.platformwebhook.PlatformWebhookService;

@Tag(name = "管理后台 - 跨境电商平台Webhook配置")
@RestController
@RequestMapping("/crossborder/platform-webhook")
@Validated
public class PlatformWebhookController {

    @Resource
    private PlatformWebhookService platformWebhookService;

    @PostMapping("/create")
    @Operation(summary = "创建跨境电商平台Webhook配置")
    @PreAuthorize("@ss.hasPermission('crossborder:platform-webhook:create')")
    public CommonResult<Long> createPlatformWebhook(@Valid @RequestBody PlatformWebhookSaveReqVO createReqVO) {
        return success(platformWebhookService.createPlatformWebhook(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新跨境电商平台Webhook配置")
    @PreAuthorize("@ss.hasPermission('crossborder:platform-webhook:update')")
    public CommonResult<Boolean> updatePlatformWebhook(@Valid @RequestBody PlatformWebhookSaveReqVO updateReqVO) {
        platformWebhookService.updatePlatformWebhook(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除跨境电商平台Webhook配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crossborder:platform-webhook:delete')")
    public CommonResult<Boolean> deletePlatformWebhook(@RequestParam("id") Long id) {
        platformWebhookService.deletePlatformWebhook(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除跨境电商平台Webhook配置")
                @PreAuthorize("@ss.hasPermission('crossborder:platform-webhook:delete')")
    public CommonResult<Boolean> deletePlatformWebhookList(@RequestParam("ids") List<Long> ids) {
        platformWebhookService.deletePlatformWebhookListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得跨境电商平台Webhook配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:platform-webhook:query')")
    public CommonResult<PlatformWebhookRespVO> getPlatformWebhook(@RequestParam("id") Long id) {
        PlatformWebhookDO platformWebhook = platformWebhookService.getPlatformWebhook(id);
        return success(BeanUtils.toBean(platformWebhook, PlatformWebhookRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得跨境电商平台Webhook配置分页")
    @PreAuthorize("@ss.hasPermission('crossborder:platform-webhook:query')")
    public CommonResult<PageResult<PlatformWebhookRespVO>> getPlatformWebhookPage(@Valid PlatformWebhookPageReqVO pageReqVO) {
        PageResult<PlatformWebhookDO> pageResult = platformWebhookService.getPlatformWebhookPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PlatformWebhookRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出跨境电商平台Webhook配置 Excel")
    @PreAuthorize("@ss.hasPermission('crossborder:platform-webhook:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPlatformWebhookExcel(@Valid PlatformWebhookPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PlatformWebhookDO> list = platformWebhookService.getPlatformWebhookPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "跨境电商平台Webhook配置.xls", "数据", PlatformWebhookRespVO.class,
                        BeanUtils.toBean(list, PlatformWebhookRespVO.class));
    }

}